/*
 * @Author: unhum 
 * @Date: 2018-06-11 16:57:16 
 * @Last Modified by: unhum
 * @Last Modified time: 2018-06-29 17:32:07
 */
 
/*
 * clickDom 绑定点击dom 例子：'.click' (String)
 * targetDom 飞往目的地dom 例子：'.target' (String)
 * t 平抛运动总共耗时，默认值为3000 单位是毫秒 (Number)
 * G 重力加速度，默认值为9.8 (Number)
 * callback 执行完的回调 (Function)
 * ---- dom ----
 * 抛体dom需要加attribute<CFly-goods-img>才会显示抛体的商品图 例子：<button CFly-goods-img="路径/xxxx.png" >添加到购物车</button> ---抛体填充
 * 抛体dom个别特殊回调 例子：<button CFly-callback="a()" >添加到购物车</button> ---抛体按钮回调 执行顺序在插件统一回调之后
 */

; (function (global) {
    "use strict";

    var CFly = function (params) {
        if (!params || !params.clickDom || !params.targetDom) return alert('必填参数不能为空');

        var that = this;
        this.clickDom = params.clickDom;
        this.targetDom = params.targetDom;
        this.t = params.t || 3000;
        this.g = params.g || 9.8;
        this.frameRate = params.frameRate || 10;
        this.goods = params.goods;
        this.callback = params.callback;

        function isArray(obj) {
            return Object.prototype.toString.call(obj) === '[object Array]';
        };

        function isFn(obj) {
            return Object.prototype.toString.call(obj) === '[object Function]';
        };

        function init() {
            function create(p, d) {
                var dom = document.createElement('div');
                dom.setAttribute('style', 'position: fixed;z-index: 999;width: 50px;height: 50px;border-radius: 50%;overflow: hidden');
                dom.innerHTML = '<img style="width: 100%; height: 100%;" alt="产品图" src="' + d.getAttribute('CFly-goods-img') + '" />';
                dom.style.left = p.x + 'px';
                dom.style.top = p.y + 'px';
                if (p.goods && p.goods.class){
                    if (isArray(p.goods.class)) {
                        var len = p.goods.class.length;
                        for (var i = 0; i < len; i++) {
                            dom.classList.add(p.goods.class[i]);
                        }
                    } else {
                        dom.classList.add(p.goods.class);
                    }
                };
                document.body.appendChild(dom);
                // 抛体运动
                flatThrowing({clickDom: d ,element: dom, position: { x: p.x, y: p.y } }, 0);
            };

            var offsetTop = function (elements) {
                var top = elements.offsetTop;
                var parent = elements.offsetParent;
                while (parent != null) {
                    top += parent.offsetTop;
                    parent = parent.offsetParent;
                };
                return top;
            };

            function move(element, position) {
                element.style.left = position.x + 'px';
                element.style.top = position.y + 'px';
            };

            function flatThrowing(p, time, SETTIMEOUT) {
                var _time = time,
                    _ctime = that.t;
                SETTIMEOUT = setTimeout(function () {
                    if (_time < _ctime) {
                        var _temp = {},
                            _target = {
                                x: Math.abs(document.querySelector(that.targetDom).offsetLeft - document.querySelector(that.targetDom).offsetWidth / 2),
                                y: offsetTop(document.querySelector(that.targetDom)) - p.position.y
                            };
                        _temp.x = p.position.x + (_time / (_ctime ? _ctime : 3000)) * _target.x;
                        _temp.y = p.position.y + _target.y * Math.pow((_time / _ctime), 2);
                        move(p.element, _temp);
                        _time = _time + 30;
                        return flatThrowing(p, _time, SETTIMEOUT);
                    } else {
                        p.element.remove();
                        if(p.clickDom.getAttribute('CFly-callback')) {
                            try{
                                (new Function(p.clickDom.getAttribute('CFly-callback')))();
                            }catch(e) {
                                console.warn('没有找到方法：', e);
                            };
                        };
                        if (that.callback && isFn(that.callback)) that.callback();                                        
                        clearInterval(SETTIMEOUT);                   
                    }
                }, that.frameRate);
            };
        
            var _clickDom, _clickDomLen;
            if (isArray(that.clickDom)) {
                var clickDomLen = that.clickDom.length;
                for (var j = 0; j < clickDomLen; j++) {
                    _clickDom.push(document.querySelector(that.clickDom[j]));
                }
            } else {
                _clickDom = document.querySelectorAll(that.clickDom);
            };
            _clickDomLen = _clickDom.length;
            for (var i = 0; i < _clickDomLen; i++) {
                _clickDom[i].onclick = function (event) {
                    var e = window.event || event;
                    create({
                        x: e.clientX - 25,
                        y: e.clientY - 25                        
                    }, this)
                };
            };
        };

        init();
    };

    if (typeof module !== 'undefined' && module.exports) module.exports = CFly;
    if (typeof define === 'function') define(function () { return CFly; });

    global.CFly = CFly;
})(this);